<?php
namespace SinoPHP\BbdBoss\Model;

use Upfor\UpforPHP\Model;
use SinoPHP\BbdBoss\Defines\StaticConfig;

class CustomerDomain extends Model {

    /**
     * 获取域名列表信息
     *
     * @param string $fields
     * @param array  $where
     * @param string $query
     * @param array  $orderBy
     * @param int    $limit
     * @param int    $offset
     * @return mixed
     */
    public function getDomainList(
        $fields = '*', $where = array(), $query = '', $orderBy = array(), $limit = null, $offset = null
    ) {

        $sql = "SELECT 
                  {$fields} 
                FROM 
                  `customer_domain`
                LEFT JOIN `customer_user` ON `customer_user`.`customer_user_id` = `customer_domain`.`customer_user_id`
                LEFT JOIN `customer_domain_template` ON `customer_domain_template`.`template_id` = `customer_domain`.`template_id`
                WHERE " . implode(' and ', $where);
        if ($query !== '') {
            $sql .= " AND (`customer_user`.`username` LIKE '%" . trim($query) . "%' OR `customer_domain`.`domain_name` LIKE '%" . trim($query) . "%') ";
        }
        if (strpos($fields, 'count') === false) {
            $sql .= " ORDER BY " . implode(' , ', $orderBy) . " LIMIT {$offset},{$limit}";
        }

        return $this->db->query($sql);
    }

    /**
     * 根据id获取域名信息
     *
     * @param $id
     * @return array
     */
    public function getDomainById($id) {

        $fields = 'D.domain_name,D.product_type,D.domain_status,D.create_time,D.update_time,D.icp,T.template_name';
        $this->db->table('customer_domain', 'D');
        $this->db->field($fields);
        $this->db->join(['customer_domain_template', 'T'], ['T.template_id' => 'D.template_id']);
        $this->db->where(['D.domain_id' => $id]);

        return $this->db->getRow();
    }

    /**
     * 获取用户列表
     *
     * @param string $fields
     * @param array  $where
     * @return array
     */
    public function getUserList($fields, $where = array()) {
        return $this->db->table('customer_user')->field($fields)->where($where)->getList();

    }

    public function getServerGroupById($where, $type) {
        if ($type == 'array') {
            return $this->db->table('server_group')->field('server_group_id')->where(['server_group_id' => $where])->getList();
        } else {
            return $this->db->table('server_group')->field('server_group_id')->where(['server_group_id' => $where])->getRow();
        }
    }

    /**
     * 绑定服务器组
     *
     * @param array $data
     * @param array $ids
     * @return bool/
     */
    public function domainBindServerGroup($data = array(), $ids = array()) {
        if (!$data) {
            return true;
        }
        $serverGroupType = StaticConfig::$ServerGroupType;
        $sql = "INSERT INTO 
                  `customer_domain_server_group`
              (`domain_id`, `server_group_id`, `server_group_type`) VALUES ";
        //多个id,绑定多个服务器组
        //拼接sql
        foreach ($ids as $id) {
            foreach ($data as $key => $item) {
                if ($key == 'backupStorage') {
                    $sql .= "({$id},{$item}," . array_search('backupStorage', $serverGroupType) . "),";
                }
                if ($key == 'masterStorage') {
                    $sql .= "({$id},{$item}," . array_search('masterStorage', $serverGroupType) . "),";
                }
                if ($key == 'cacheList') {
                    foreach ($item as $cache) {
                        $sql .= "({$id},{$cache}," . array_search('cacheList', $serverGroupType) . "),";
                    }
                }
            }
        }
        $this->db->query(substr($sql, 0, -1));
    }

    /**
     * 获取可用的缓存服务器组
     *
     * @param int $cacheId
     * @return array
     */
    public function getCacheServerGroupId($cacheId) {
        $this->db->table('server_group');
        $this->db->field('server_group_id');
        $this->db->join('server_group_role', ['server_group.server_role_id' => 'server_group_role.server_role_id']);
        $this->db->where(['server_group_role.type' => $cacheId, 'status' => 1]);
        $res = $this->db->getList();

        return $res;
    }
}